package com.android.djlmapp.tools.kaika;

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.listener.read.GetPhotoUrlListener;
import com.android.djlmapp.listener.read.SubReadDataListener;
import com.android.djlmapp.model.readcard.SubCardDataInfo;
import com.android.djlmapp.tools.global.PrefUtils;
import com.android.djlmapp.tools.read.GetPhotoUrlUtils;
import com.android.readcard.bean.CardData;
import com.android.readcard.bean.ReadCardInfo;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.io.OutputStream;

/**
 * 上传读卡数据工具类
 * 通用
 */
public class SubReadCardDataUtils {

    /**
     * 获取经办人数据
     *
     * @param readInfo
     * @param cardData
     * @param chuanma
     */
    public static void getJingBanRenData(ReadCardInfo readInfo, CardData cardData,
                                         String chuanma, SubReadDataListener mSubReadDataListener) {
        Gson gson = new Gson();
        String subData = gson.toJson(cardData); // 先将数据转json然后加密上传
        subData = Base64.encodeToString(subData.getBytes(), Base64.DEFAULT);
        // 然后提交数据到服务器
        MyApplication.getInstance().getHttpTools().submitIdCard(subData, chuanma, new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    Gson gson = new Gson();
                    SubCardDataInfo idInfo = gson.fromJson(response.body(), SubCardDataInfo.class);
                    int code = idInfo.getCode();
                    if (code == 200) {
                        String needJbr = idInfo.getNeed_jbr();
                        String idPath = readInfo.getIdPath(); // 获取头像路径

//                        String idPath = readInfo.getPhotoHeard(); // 获取头像路径
//                        byte[] bytes;
//                        bytes = Base64.decode(idPath, Base64.NO_WRAP);
//
//                        try {
//                            OutputStream outputStream = MyApplication.getInstance().openFileOutput("imgHead.png", Context.MODE_PRIVATE);
//                            outputStream.write(bytes);
//                            outputStream.flush();
//                            outputStream.close();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        File file = new File(MyApplication.getInstance().getFilesDir(), "imgHead.png");
//                        getPhotoUrl(needJbr, mSubReadDataListener, file.getAbsolutePath());
                        getPhotoUrl(needJbr, mSubReadDataListener, idPath);
                    } else {
                        mSubReadDataListener.subErr(idInfo.getMsg());
                    }
                } catch (Exception e) {

                    mSubReadDataListener.subErr("数据解析异常1");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                mSubReadDataListener.subErr("网络或服务器错误1");
            }

        });
    }

    private static void getPhotoUrl(String needJbr, SubReadDataListener mSubReadDataListener, String idPath) {
        // 存储经办人状态值
        PrefUtils.putString("NEED_JBR", needJbr);
        // 获取身份证头像url
        GetPhotoUrlUtils.getPhotoUrl(idPath, new GetPhotoUrlListener() {
            @Override
            public void backPhotoUrl(int backType, String photoUrl) {
                if (backType == 1) {
                    mSubReadDataListener.subPhotoSuc(needJbr, photoUrl);
                } else {
                    mSubReadDataListener.subErr(photoUrl);
                }
            }
        });
    }
}
